# -*- coding=utf-8 -*-
'''
Description: 
version: 1.0.0
Author: peng2.zhang
Date: 2021-05-12 10:20:46
LastEditors: peng2.zhang
LastEditTime: 2021-12-03 10:39:22
'''
import time
import ujson
from ci_conclude.models.db import DBConnection
from ci_conclude.models.models import SonarStatistic
from ci_conclude.services.base import BaseService
from ci_conclude.utils.error_code import ERROR
from ci_conclude.utils.validations import Validator, validate
from voluptuous import Schema, ALLOW_EXTRA, Required, All, Any, Coerce, Length
from ci_conclude.log import logger


class SonarStatisticService(BaseService,Validator):

    def _get_or_create(self, defaults, **kwargs):
        filtered_kwargs = SonarStatistic.filter_kwargs(kwargs)
        if filtered_kwargs:
            filter_list = self.db.query(SonarStatistic).filter_by(**defaults)
            if filter_list.count() > 0:
                filter_list.update(filtered_kwargs)
                first = filter_list.first()
            else:
                defaults.update(filtered_kwargs)
                first = SonarStatistic(**defaults)
            self.db.add(first)
            self.db.flush()
            self.db.commit()
    
    @classmethod
    def get_or_create(cls, defaults, **kwargs):
        with DBConnection() as db_conn:
            svc = cls(db_conn)
            res = svc._get_or_create(defaults, **kwargs)
